package com.newbsd.web.maventest.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.newbsd.web.maventest.common.pojo.EasyUIDataGridJSON;
import com.newbsd.web.maventest.pojo.Ticket;
import com.newbsd.web.maventest.service.UserQueryDao;
import com.newbsd.web.maventest.service.impl.UserQueryDaoImpl;

/**
 * Servlet implementation class TicketQueryServlet
 * @author wangjing
 */
@WebServlet("/TicketQueryServlet")
public class TicketQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private UserQueryDao uqd = new UserQueryDaoImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TicketQueryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		
//		String origin = request.getParameter("origin");
//		String destination = request.getParameter("destination");
		String origin = "南京南";
		String destination = "北京北";
		Integer	page;
		Integer rows;
		if (null ==request.getParameter("page") && null ==request.getParameter("rows") ) {
				page = 1;
				rows = 10;
		} 
		else {
			 page = Integer.valueOf(request.getParameter("page"));
			 rows = Integer.valueOf(request.getParameter("rows"));
		}
		
		List<Ticket> ticketlist = uqd.QueryTickets(origin, destination,page,rows);

		int totalRecords = uqd.QueryTicNum(origin, destination);
		System.out.println(totalRecords);
		
		
		
		EasyUIDataGridJSON responseObject = new EasyUIDataGridJSON();
		//responseObject.setTotal(ticketlist.size());
		responseObject.setTotal(totalRecords);
		responseObject.setRows(ticketlist);
		
		response.setContentType("application/json");
		response.getWriter().append(JSON.toJSONString(responseObject));
		System.out.println(JSON.toJSONString(ticketlist));
		
//		request.setAttribute("ticketList", ticketlist);
//		request.getRequestDispatcher("usertickets.jsp").forward(request,
//				response);
	}

}
